<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.search" size="default" placeholder="请输入角色名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="query()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<!-- <el-button size="default" type="success" class="ml10" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button> -->
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<!-- <el-table-column prop="id" label="授权编号" show-overflow-tooltip  ></el-table-column> -->
				<!-- <el-table-column prop="roleId" label="角色编号" show-overflow-tooltip width="60" ></el-table-column> -->
				<el-table-column prop="role" label="角色标识" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleName" label="角色名称" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="roleIntro" label="角色描述" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="roleStatus" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.roleStatus">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="permissionId" label="权限编号" show-overflow-tooltip width="60" ></el-table-column> -->
				<el-table-column prop="permission" label="权限标识" show-overflow-tooltip></el-table-column>
				<el-table-column prop="permissionName" label="权限名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="permissionIntro" label="权限描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="permissionStatus" label="权限状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.permissionStatus">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="authorizeTime" label="授权时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="authorizeStatus" label="授权状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.authorizeStatus">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.roleName === ''" size="small" text type="primary" @click="onOpenEditRole('edit', scope.row)"
							>修改</el-button
						>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-if="scope.row.authorizeStatus">删除</el-button>
						<el-button size="small" text type="primary" @click="onEnable(scope.row)" v-else>重启</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="systemRole">
import { ElMessageBox, ElMessage } from 'element-plus';
import axios from 'axios';
import api from "/@/api.js";
// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/rolePermission/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive({
	search:'',
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});


// 初始化表格数据
const getTableData = () => {
	/*userApi().getAllUserByPage({"page":1,"pageSize":10}).then(res=>{}
	).catch(function(response){
		console.log("请求发送成功");
		console.log(response);
	}
	).finally(function(){
		console.log("2");
	}
		
	);*/

	axios({
		method: 'post',
		url: api.commonUrl+'/rolePermission/getallrolepermissionbypage',
		// data: {
		// 	page: 1,
		// 	pageSize: 10,
		// },
		data: `page=` + state.tableData.param.pageNum + `&pageSize=` + state.tableData.param.pageSize,
	}).then(function (response) {
			if(response.data.code === 200){
				state.tableData.loading = true;
				const data = [];
				for (let i = 0; i < response.data.data.records.length; i++) {
					//console.log(user);
					data.push({
						id: response.data.data.records[i].id,
						roleId:response.data.data.records[i].roleId,
						role: response.data.data.records[i].role,
						roleName: response.data.data.records[i].roleName,
						roleIntro: response.data.data.records[i].roleIntro,
						roleStatus: response.data.data.records[i].roleStatus === '1' ? true : false,
						permissionId: response.data.data.records[i].permissionId,
						permission: response.data.data.records[i].permission,
						permissionName: response.data.data.records[i].permissionName,
						permissionIntro: response.data.data.records[i].permissionIntro,
						permissionStatus: response.data.data.records[i].permissionStatus === '1' ? true : false,
						authorizeTime: response.data.data.records[i].authorizeTime,
						authorizeStatus: response.data.data.records[i].authorizeStatus === '1'? true : false,
					});
				}
			state.tableData.data = data;
			state.tableData.total = response.data.data.total;
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
	});
};

const query = () =>{
	axios({
		method: 'get',
		url: api.commonUrl+'/rolePermission/getbyrolename',
		params: {
			roleName:state.search
		},
		
	}).then(function (response) {
			if(response.data.code === 200){
				state.tableData.loading = true;
				const data = [];
				for (let i = 0; i < response.data.data.rolePermissionList.length; i++) {
					//console.log(user);
					data.push({
						id: response.data.data.rolePermissionList[i].id,
						roleId:response.data.data.rolePermissionList[i].roleId,
						role: response.data.data.rolePermissionList[i].role,
						roleName: response.data.data.rolePermissionList[i].roleName,
						roleIntro: response.data.data.rolePermissionList[i].roleIntro,
						roleStatus: response.data.data.rolePermissionList[i].roleStatus === '1' ? true : false,
						permissionId: response.data.data.rolePermissionList[i].permissionId,
						permission: response.data.data.rolePermissionList[i].permission,
						permissionName: response.data.data.rolePermissionList[i].permissionName,
						permissionIntro: response.data.data.rolePermissionList[i].permissionIntro,
						permissionStatus: response.data.data.rolePermissionList[i].permissionStatus === '1' ? true : false,
						authorizeTime: response.data.data.rolePermissionList[i].authorizeTime,
						authorizeStatus: response.data.data.rolePermissionList[i].authorizeStatus === '1'? true : false,
					});
				}
			state.tableData.data = data;
			state.tableData.total = response.data.data.rolePermissionList.length;
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
	});
};


// 打开新增角色弹窗
const onOpenAddRole = (type) => {
	roleDialogRef.value.openDialog(type);
};
// 打开修改角色弹窗
const onOpenEditRole = (type, row) => {
	roleDialogRef.value.openDialog(type, row);
};
// 删除记录
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将删除该记录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios(
			{
				method: 'post',
				url: api.commonUrl + '/rolePermission/deleterolepermissionbyid',
				data: {
					id: row.id,
				},
				headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
			}
		)
		.then(function (response) {
			ElMessage.success('删除成功');
			getTableData();
		});
		})
		.catch(() => {});
};

// 重启记录
const onEnable = (row) => {
	ElMessageBox.confirm(`此操作将重启该记录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios(
			{
				method: 'post',
				url: api.commonUrl + '/rolePermission/restartrolepermissionbyid',
				data: {
					id: row.id,
				},
				headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
			}
		)
		.then(function (response) {
			ElMessage.success('重启成功');
			getTableData();
		});
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
